package eteam.aps.systemservice.dal;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import eteam.aps.systeminterface.model.entity.FunaclEntity;
import eteam.aps.systemservice.dal.mapper.IFunaclMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 权限数据操作类
 */
@Service
public class FunaclDal {
    @Autowired
    private IFunaclMapper funaclMapper;

    /**
     * 获取用户操作权限列表
     *
     * @param userid
     * @return
     */
    public List<String> getOperationsByUserId(String userid) {
        return funaclMapper.selectObjs(new QueryWrapper<FunaclEntity>()
                .inSql("FUserId", String.format("select FGroupId from sp_groupuser where FUserId='%s'", userid))
                .select("FOperation")
        ).stream().map(p -> p.toString()).distinct().collect(Collectors.toList());
    }

    /**
     * 根据角色ID删除权限
     *
     * @param roleId
     * @return
     */
    public Integer deleteByRoleId(String roleId) {
        return funaclMapper.delete(new QueryWrapper<FunaclEntity>().eq("FUserId", roleId));
    }

    /**
     * 保存角色权限
     *
     * @param funcalList
     * @return
     */
    public void createFunacl(List<FunaclEntity> funcalList) {
        for (FunaclEntity funcal : funcalList) {
            funaclMapper.insert(funcal);
        }
    }
}
